<?php
// +----------------------------------------------------------------------
// | Description: 用户
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\admin\model\purchasing;

use app\admin\model\Common;

class Post extends Common
{

    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如微信模块用weixin作为数据表前缀
     */
    protected $name = 'admin_bidding';

    /**
     * [getDataList 获取列表]
     * @linchuangbin
     * @DateTime  2017-02-10T21:07:18+0800
     * @return    [array]
     */

    public function getDataList($id)
    {
        $map = [];
        $data=[];
        $lastOne=[];
        $userInfo = $GLOBALS['userInfo'];
        $company=$this->getCompany($userInfo['structure_id']);
        if($id){
            $map['id']=$id;
            $data = $this->where($map)->find();
        }else{
            $lastOne=$this->order('id desc ')->page(1)->find();
        }
        if(!$data){
            if($lastOne['serial']){
                $serial=str_replace($company['code'],'',$lastOne['serial']);
                $data['serial']=$company['code'].(intval($serial)+1);
            }else{
                $data['serial']=$company['code'].date("Y").'0000001';
            }
        }
        $data['companyName']=$company['name'];
        $data['area_code']=json_decode($data['area_code']);
        $data['purchase_category']=json_decode($data['purchase_category']);
        return $data;
    }
    /**
     * 创建投标项目
     * @param  array $param [description]
     */
    public function createData($param)
    {
        // 验证
        $validate = validate($this->name);
        if (!$validate->check($param)) {
            $this->error = $validate->getError();
            return false;
        }
        $company=$this->getCompany($GLOBALS['userInfo']['structure_id']);
        $this->startTrans();
        try {
            unset($param['companyName']);
            $param['area_code']=json_encode($param['area_code']);
            $param['purchase_category']=json_encode($param['purchase_category']);
            $param['user_id'] = $GLOBALS['userInfo'][id];
            $param['structure_id'] = $company[id];
            $this->data($param)->allowField(true)->save();
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->rollback();
            $this->error = '添加失败';
            return false;
        }
    }
    public function getBidTime($param)
    {
        if(empty($param['bid_id'])){
            $this->error='发生致命错误！0x00002';
            return false;
        }
        $data = $this->where(array('id'=>$param['bid_id']))->find();
        if(!$data){
            $this->error='发生致命错误！0x00002';
            return false;
        }
        return $data;
    }

    function getCompany($structure_id)
    {
        $structureModel = model('Structure');
        $structure = $structureModel->getDataById($structure_id);
        if($structure['pid']!=0){
            while ($structure['pid']!=0){
                $structure = $structureModel->getDataById($structure['pid']);
            }
        }
        return $structure;
    }
}
